import 'package:collection/collection.dart';
import 'package:flutter_axrjc/datasource/network/api/LightApi.dart';
import 'package:flutter_axrjc/datasource/network/bean/CheckLogBean.dart';
import 'package:flutter_axrjc/datasource/network/request/exp.dart';
import 'package:flutter_axrjc/util/Util.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:get/get.dart';

import 'state.dart';

class CheckLogLogic extends GetxController {
  final CheckLogState state = CheckLogState();
  @override
  void onInit() {
    super.onInit();
    getLog();
  }

  Future<void> getLog() async {
    state.list.clear();
    try {
      List<CheckLogBean> list = await LightApi.getLogList(
          queryStartTime: Util.date2String(
                state.startTime.value,
              ) ??
              '',
          queryEndTime: Util.date2String(
                state.endTime.value,
              ) ??
              '');
      state.list.addAll(groupBy(
          list,
          (p0) => Util.date2String(
              Util.string2Date("${p0.detectionStartTime}")!)).entries);
    } on ApiException catch (e) {
      EasyLoading.showToast(e.message);
    } catch (e) {
      print(e);
    }
  }
}
